<?php

namespace X3D\PHPExample\MathMVC;

class MathController extends Controller
{
    public function index()
	{

		$a = isset($_GET['a']) ? trim($_GET['a']) : null;
		$b = isset($_GET['b']) ? trim($_GET['b']) : null;
		
		if (!is_numeric($a) || !is_numeric($b)) { $this->error('无效数据'); }
	
		$model = new Addition($a, $b);
	
		$result = $model->op();
		
		$this->assign('result', $result);
	
		$this->display();
	}
}

interface MathInterface
{
	public function op();
}

class Addition extends Model implements MathInterface
{
	protected $a;
	protected $b;
	
	public function __construct(float $a, float $b)
	{
		$this->setA($a);
		$this->setB($b);
	}
	
	public function setA(float $a)
	{
		$this->a = $a;
	}
	
	public function setB(float $b)
	{
		$this->b = $b;
	}
	
	public function getA(): $a
	{
		return $this->a;
	}
	
	public function getB($b): float
	{
		return $this->b;
	}
	
	public function op(): float
	{
		return $a + $b;
	}
}

class Model
{
	
}

class View
{
	protected $data = [];
	
	public function assign(string $name, $value)
	{
		$this->data[$name] = $value;
	}
	
	public function render($tplFile)
	{
		var_dump($data);
		Application::end();
	}
	//......
}

class Controller
{
	protected $view = new View;
	
	public function assign($name, $value)
	{
		$this->view->assign($name, $value);
	}
	
	public function display($tplFile)
	{
		$this->view->render($tplFile);
	}
	
	public function error($msg)
	{
		$this->view->assign('msg', $msg);
		
		$this->view->render('error');
	}
}


class Application
{
	public static function end(int $code = 0)
	{
		exit($code);
	}
}